//
//  EDSGuideViewController.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/8/16.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSGuideViewController.h"
#import "EDSGuideViewCell.h"
#import "EDSGuideViewManager.h"

@interface EDSGuideViewController () <UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) UIButton *dismissButton;

@end

@implementation EDSGuideViewController

- (void)dealloc
{
    
}

- (void)initWithVar
{
    [super initWithVar];
    
}

- (void)initWithSubView
{
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.baseCollectionView.collectionViewLayout = layout;
    
    self.baseCollectionView.bounces = NO;
    self.baseCollectionView.pagingEnabled = YES;
    self.baseCollectionView.showsVerticalScrollIndicator = NO;
    self.baseCollectionView.showsHorizontalScrollIndicator = NO;
    self.baseCollectionView.backgroundColor = [UIColor clearColor];
    
    [self.baseCollectionView registerClass:[EDSGuideViewCell class] forCellWithReuseIdentifier:[EDSGuideViewCell className]];
    
    
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.userInteractionEnabled = NO;
    self.pageControl.hidesForSinglePage = YES;
    self.pageControl.numberOfPages = [EDSGuideViewManager manager].images.count;
    self.pageControl.pageIndicatorTintColor = [EDSGuideViewManager manager].pageIndicatorTintColor;
    self.pageControl.currentPageIndicatorTintColor = [EDSGuideViewManager manager].currentPageIndicatorTintColor;
    [self.view addSubview:self.pageControl];
    
    if ([EDSGuideViewManager manager].shouldDismissWhenDragging == NO)
    {
        self.dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.dismissButton.hidden = YES;
        [self.dismissButton setImage:[EDSGuideViewManager manager].dismissButtonImage forState:UIControlStateNormal];
        [self.dismissButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self.dismissButton sizeToFit];
        [self.view addSubview:self.dismissButton];
    }
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.baseCollectionView.frame = self.view.bounds;
    CGSize size = [self.pageControl sizeForNumberOfPages:self.pageControl.numberOfPages];
    self.pageControl.frame = CGRectMake(CGRectGetWidth(self.view.frame) * 0.5, CGRectGetHeight(self.view.frame) - size.height, size.width, size.height);
    
    self.dismissButton.center = [EDSGuideViewManager manager].dismissButtonCenter;
    
    
}

- (void)dismiss
{
    if (self.willDismissHandler)
    {
        self.willDismissHandler();
    }
}

- (BOOL)prefersHomeIndicatorAutoHidden
{
    return YES;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([EDSGuideViewManager manager].shouldDismissWhenDragging)
    {
        return [EDSGuideViewManager manager].images.count + 1;
    }
    return [EDSGuideViewManager manager].images.count;
}

- (__kindof UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    EDSGuideViewCell *cell = [EDSGuideViewCell cellForCollectionView:collectionView forIndexPath:indexPath];
    if (indexPath.row >= [EDSGuideViewManager manager].images.count)
    {
        cell.imageView.image = nil;
    }
    else
    {
        cell.imageView.image = [EDSGuideViewManager manager].images[indexPath.row];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.bounds.size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    long current = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
    
    self.pageControl.currentPage = lroundf(current);
    
    if ([EDSGuideViewManager manager].shouldDismissWhenDragging == NO)
    {
        
        self.dismissButton.hidden = [EDSGuideViewManager manager].images.count != current + 1;
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [EDSGuideViewManager manager].supportedInterfaceOrientation;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if ([EDSGuideViewManager manager].shouldDismissWhenDragging == YES)
    {
        int current = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
        if (current == [EDSGuideViewManager manager].images.count)
        {
            [self dismiss];
        }
    }
}

@end































